package com.youhome.business.controller;


import com.youhome.business.dto.DTOCompanyUpdate;
import com.youhome.business.dto.DTOHomePageUpdate;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.service.model.BCompany;
import com.youhome.service.model.BUser;
import com.youhome.service.model.Principal;
import com.youhome.service.service.BCompanyService;
import com.youhome.service.service.BUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by chen365 on 2017/10/24.
 */
@RestController
@RequestMapping(value = "/company")
@Api(tags = "公司")
public class CompanyController {

    @Autowired
    private BUserService bUserService;
    @Autowired
    private BCompanyService bCompanyService;

    @ApiOperation(value = "公司详情", notes = "获取公司信息详情", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/details")
    public Object company(@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result(ResultCode.SUCCESS, bCompanyService.selectDetails(principal));
    }

    @ApiOperation(value = "修改公司信息", notes = "修改公司信息", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/update")
    public Object update(@ApiParam @Validated DTOCompanyUpdate dtoCompanyUpdate,@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        ModelMapper modelMapper = new ModelMapper();
        return new Result(
                ResultCode.SUCCESS,
                bCompanyService.updateCompany(principal, modelMapper.map(dtoCompanyUpdate, BCompany.class))
        );
    }

    @ApiOperation(value = "修改公司落地页信息", notes = "修改公司落地页信息", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/updatehomepage")
    public Object updateHomePage(@ApiParam @Validated DTOHomePageUpdate dtoHomePageUpdate,@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        ModelMapper modelMapper = new ModelMapper();

        return new Result(
                ResultCode.SUCCESS,
                bCompanyService.updateHomePage(principal, modelMapper.map(dtoHomePageUpdate, BCompany.class))
        );

    }

}
